package com.rst.web;

import java.util.Collection;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.rst.model.UserDetailsDTO;

@Controller
public class AuthenticationController {
	
	private Logger logger = Logger.getLogger(AuthenticationController.class);
	

	@RequestMapping("/login.htm")
	public ModelAndView getLoginPage(){
		logger.debug("inside getLoginPage method ....");
		return new ModelAndView("login/login");
	}
	
/*	@RequestMapping("/authenticateSuccess.htm")
	public ModelAndView getDashboard() {
		ModelAndView modelAndView = new ModelAndView("login/newDashboard");
		UserDetailsDTO userDetailsDTO = (UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();		
		modelAndView.addObject("user", userDetailsDTO);
		return modelAndView;
	}
*/	

	@RequestMapping("/authenticateSuccess.htm")
	public ModelAndView getDashboard() {
		ModelAndView modelAndView = new ModelAndView("login/dashboard");
		String role = null;
		UserDetailsDTO userDetailsDTO = (UserDetailsDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		Collection<GrantedAuthority> au =  SecurityContextHolder.getContext().getAuthentication().getAuthorities();
		for (@SuppressWarnings("rawtypes")
		Iterator iterator = au.iterator(); iterator.hasNext();) {
			GrantedAuthority grantedAuthority = (GrantedAuthority) iterator	.next();
			role = grantedAuthority.getAuthority();		
		}

		modelAndView.addObject("user", userDetailsDTO);
		modelAndView.addObject("role", role);
		return modelAndView;
	}


}